setwd("~/Dropbox/CHIPS")
library(haven);library(car);library(ggplot2);library(stringr)

d <- read_dta("FIGURE1 ELECTIONS.dta")
names(d)


d$elected <- recode(d$SSC_ELECT,"0:50=0;50.001:100=1;NA=2")
d$System <- recode(d$SSC_ELECT,"0:50='Majority Believe Appointed';50.001:100='Majority Believe Elected';NA='U.S. Supreme Court'")
d$rounded <- str_remove(format(round(d$sscsupx, digits=2), nsmall = 2), "^0+")
d$se <- d$sscsupsd/sqrt(d$nn)

pdf("Figure1_v3.pdf",width=11,height=6,paper='special') 
ggplot(d, aes(x=reorder(state, sscsupx), y=sscsupx, fill=System,color=System )) + 
  geom_bar(stat="identity",lwd=.3) +
  geom_label(
    aes(label = rounded,y=sscsupx/2), 
    ## make labels left-aligned
    vjust=0,
    color="black",size=1.8,fill="white",
  ) +
  geom_errorbar( aes(x=reorder(state, sscsupx), ymin=sscsupx-1.96*se, ymax=sscsupx+1.96*se), width=0.6, 
                 colour="gray60", alpha=1, size=.6) +
  theme_bw() + xlab("") + ylab("Average Diffuse Support") +
  theme(legend.position = "bottom",axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1,color="black"),
        axis.text.y = element_text(color="black"), legend.title = element_text(size = 8), 
        legend.text = element_text(size = 8)) + 
  scale_fill_manual(values=c("gray80","gray40","white")) + 
  scale_color_manual(values=c("black","black","black")) + 
  scale_y_continuous(limits=c(0,.6),breaks = c("0" =0 , ".20"= 0.2, ".40" = .4, ".60" = 0.6)) +
  theme(legend.key.size = unit(.2, 'cm')) 
dev.off()

wi <- data.frame(activities=c("Run TV ads criticizing opponents",
                              "Seek campaign donations",
                              "Seek endorsements from groups",
                              "Encourage spending by outside interest groups",
                              "Announce how they would vote on legal issues",
                              "Endorse candidates for other elected offices",
                              "Participate in televised debates"),
                 pctExpect=c(82.1,70.4,70.11,70.1,69.4,46.4,44.8),
                 pctApprove=c(29.2,62.8,55.3,11.8,76.2,44.7,88.1))

pdf("Figure2_v1.pdf",width=7,height=4,paper='special') 
ggplot(wi, aes(x=reorder(activities, -pctExpect), y=pctExpect )) + 
  geom_bar(stat="identity",lwd=.3) +
  geom_label(
    aes(label = round(pctExpect,1),y=pctExpect/2), 
    ## make labels left-aligned
    vjust=0,
    color="black",size=4,fill="white",
  ) +
  theme_bw() + xlab("") + ylab("Percentage Expecting the Activity") +
  ylim(0,100) + theme(axis.text.x = element_text(color="black"),
                      axis.text.y = element_text(color="black"))  +
  scale_x_discrete(labels = function(x) str_wrap(x, width = 12))
  #scale_y_continuous(limits=c(0,100),breaks = c("0" =0 , "20"= 20, "".40" = .4, ".60" = 0.6"40"")) +
  #theme(legend.key.size = unit(.2, 'cm')) 
dev.off()

pdf("Figure3_v1.pdf",width=7,height=4,paper='special') 
ggplot(wi, aes(x=reorder(activities, -pctApprove), y=pctApprove )) + 
  geom_bar(stat="identity",lwd=.3) +
  geom_label(
    aes(label = round(pctApprove,1),y=(pctApprove/2)-3), 
    ## make labels left-aligned
    vjust=0,
    color="black",size=4,fill="white",
  ) +
  theme_bw() + xlab("") + ylab("Percentage Approving of the Activity") +
  ylim(0,100) + theme(axis.text.x = element_text(color="black"),
                      axis.text.y = element_text(color="black"))  +
  scale_x_discrete(labels = function(x) str_wrap(x, width = 12))
dev.off()

comp <- data.frame(activities=c("Run TV ads criticizing opponents",
                              "Seek campaign donations",
                              "Seek endorsements from groups",
                              "Encourage spending by outside interest groups",
                              "Announce how they would vote on legal issues",
                              "Endorse candidates for other elected offices",
                              "Participate in televised debates",
                              "Run TV ads criticizing opponents",
                              "Seek campaign donations",
                              "Seek endorsements from groups",
                              "Encourage spending by outside interest groups",
                              "Announce how they would vote on legal issues",
                              "Endorse candidates for other elected offices",
                              "Participate in televised debates"),
                 pctExpect=c(82.1,70.4,70.11,70.1,69.4,46.4,44.8,
                             47,51,49,35,55,33,37),
                 pctApprove=c(29.2,62.8,55.3,11.8,76.2,44.7,88.1,
                              29,64,59,27,77,54,85),
                 Sample=c(rep("Wisconsin Survey",7),rep("National Survey",7)))
comp$Sample <- factor(comp$Sample,levels=c("Wisconsin Survey","National Survey"),ordered=T)
comp$activities <- factor(comp$activities,levels=c("Run TV ads criticizing opponents",
                                                   "Seek campaign donations",
                                                   "Seek endorsements from groups",
                                                   "Encourage spending by outside interest groups",
                                                   "Announce how they would vote on legal issues",
                                                   "Endorse candidates for other elected offices",
                                                   "Participate in televised debates"))

pdf("FigureD2_v1.pdf",width=8,height=6,paper='special') 
ggplot(comp, aes(x=activities, y=pctExpect, fill=Sample,color=Sample )) + 
  geom_bar(stat="identity",lwd=.3,position="dodge") +
  geom_label(
    aes(label = round(pctExpect,0),y=pctExpect/2), position=position_dodge2(width=.9),
    color="black",size=3,fill="white") +
  theme_bw() + xlab("") + ylab("Percentage Expecting the Activity") +
  theme(legend.position = "bottom",axis.text.x = element_text(color="black"),
        axis.text.y = element_text(color="black"), legend.title = element_text(size = 8), 
        legend.text = element_text(size = 8)) + 
  scale_fill_manual(values=c("gray40","gray70","gray40")) + 
  scale_color_manual(values=c("black","black","black")) + ylim(0,100) +
  scale_x_discrete(labels = function(x) str_wrap(x, width = 12))
  #scale_y_continuous(limits=c(0,100),breaks = c("0" =0 , ".20"= 0.2, ".40" = .4, ".60" = 0.6)) +
  #theme(legend.key.size = unit(.2, 'cm')) 
dev.off()

comp1 <- data.frame(activities=c("Run TV ads criticizing opponents",
                                "Seek campaign donations",
                                "Seek endorsements from groups",
                                "Encourage spending by outside interest groups",
                                "Announce how they would vote on legal issues",
                                "Endorse candidates for other elected offices",
                                "Participate in televised debates",
                                "Run TV ads criticizing opponents",
                                "Seek campaign donations",
                                "Seek endorsements from groups",
                                "Encourage spending by outside interest groups",
                                "Announce how they would vote on legal issues",
                                "Endorse candidates for other elected offices",
                                "Participate in televised debates"),
                   pctExpect=c(82.1,70.4,70.11,70.1,69.4,46.4,44.8,
                               47,51,49,35,55,33,37),
                   pctApprove=c(29.2,62.8,56,11.8,76.2,44.7,88.1,
                                29,64,59,27,77,54,85),
                   Sample=c(rep("Wisconsin Survey",7),rep("National Survey",7)))
comp1$Sample <- factor(comp$Sample,levels=c("Wisconsin Survey","National Survey"),ordered=T)
comp1$activities <- factor(comp$activities,levels=c("Participate in televised debates",
                                                   "Announce how they would vote on legal issues",
                                                   "Seek campaign donations",
                                                   "Seek endorsements from groups",
                                                   "Endorse candidates for other elected offices",
                                                   "Run TV ads criticizing opponents",
                                                   "Encourage spending by outside interest groups"))

pdf("FigureD1_v1.pdf",width=8,height=6,paper='special') 
ggplot(comp1, aes(x=activities, y=pctApprove, fill=Sample,color=Sample )) + 
  geom_bar(stat="identity",lwd=.3,position="dodge") +
  geom_label(
    aes(label = round(pctApprove,0),y=pctApprove/2), position=position_dodge2(width=.9),
    color="black",size=3,fill="white") +
  theme_bw() + xlab("") + ylab("Percentage Approving of the Activity") +
  theme(legend.position = "bottom",axis.text.x = element_text(color="black"),
        axis.text.y = element_text(color="black"), legend.title = element_text(size = 8), 
        legend.text = element_text(size = 8)) + 
  scale_fill_manual(values=c("gray40","gray70","gray40")) + 
  scale_color_manual(values=c("black","black","black")) + ylim(0,100) +
  scale_x_discrete(labels = function(x) str_wrap(x, width = 12))
#scale_y_continuous(limits=c(0,100),breaks = c("0" =0 , ".20"= 0.2, ".40" = .4, ".60" = 0.6)) +
#theme(legend.key.size = unit(.2, 'cm')) 
dev.off()